
################################################################################################################################################################
#This R-File replicates the results presented in 
#Figure A.8
#Figure A.7
#Version 1.0 by Dennis Kolcava
################################################################################################################################################################
#################################

rm(list = ls())
gc()

#setwd(...)



library(foreign)
library(broom)
library(dotwhisker)
library(plyr)
library(ggplot2)
library(lmtest)
library(sandwich)
library(cjoint)
library(psych)
library(car)
library(cowplot)
library(binom)
library (readstata13)


##############################################
# LOAD DATA, SUBSET, RENAME AND RECODE VARS, whereby
# Subset = High-income countries 
# Selected variables:
#v9 = less regulation for business, 1=pro, 5=con
#v13 = govt spending on environment should be 1=much more, 5=much less
#v30 = responsibility of govt. to make laws to make industry not destroy evt, 1=yes, 4=no
#v56 = do major private companies comply with regulation?, 1=always, 4=never
# Drop categories 8 (can't choose) and 9 (no answer)
# Recode = all scales such that higher values=more regulation/more spending/more laws/companies do not comply
##############################################
data <- read.dta("issp_rep.dta")

data2 <- subset(data, c_alphan == "AU"|c_alphan=="BE"|c_alphan=="DK"|c_alphan=="FI"|
               c_alphan=="FR"|c_alphan=="DE"|c_alphan=="IS"|c_alphan=="NZ"|
               c_alphan=="NO"|c_alphan=="SE"|c_alphan=="CH"|c_alphan=="GB-GBN"|
               c_alphan=="US")

data2=rename(data2, c("v9"="regulation"))
data2=rename(data2, c("v13"="spending"))
data2=rename(data2, c("v30"="laws"))
data2=rename(data2, c("v56"="compliance"))

data2$country <- ifelse(data2$country=="AU-Australia", "Australia",
                           ifelse(data2$country=="Be-Belgium", "Belgium",
                                  ifelse(data2$country=="DK-Denmark", "Denmark",
                                         ifelse(data2$country =="FI-Finland", "Finland",
                                                ifelse(data2$country == "FR-France", "France",
                                                       ifelse(data2$country == "DE-Germany", "Germany",
                                                              ifelse(data2$country == "IS-Iceland", "Iceland",
                                                                     ifelse(data2$country == "NZ-New Zealand", "New Zealand",
                                                                            ifelse(data2$country == "NO-Norway", "Norway",
                                                                                   ifelse(data2$country == "SE-Sweden", "Sweden",
                                                                                          ifelse(data2$country == "CH-Switzerland", "Switzerland",
                                                                                                 ifelse(data2$country == "GB-Great Britain and/or United Kingdom", "United Kingdom",
                                                                                                        ifelse(data2$country == "US-United States", "United States",
                                                                                                               NA)))))))))))))


data2$c_alphan<- replace(data2$c_alphan, which(data2$c_alphan=="GB-GBN"), "GB")


data2$regulation <- ifelse(data2$regulation=="Strongly in favour of", "1 (for)",
                          ifelse(data2$regulation=="In favour of", "2",
                                 ifelse(data2$regulation=="Neither in favour of nor against", "3",
                                        ifelse(data2$regulation =="Against", "4",
                                               ifelse(data2$regulation == "Strongly against", "5 (against)",
                                                       NA)))))
data2$regulation <- as.factor(data2$regulation)


data2$spending <- ifelse(data2$spending=="Spend much more", "5 (more)",
                           ifelse(data2$spending=="Spend more", "4",
                                  ifelse(data2$spending=="Spend the same as now", "3",
                                         ifelse(data2$spending =="Spend less", "2",
                                                ifelse(data2$spending == "Spend much less", "1 (less)",
                                                       NA)))))


data2$laws <- ifelse(data2$laws=="Definitely should be", "4 (agree)",
                           ifelse(data2$laws=="Probably should be", "3",
                                  ifelse(data2$laws=="Probably should not be", "2",
                                         ifelse(data2$laws =="Definitely should not be", "1 (disagree)",
                                                     NA))))


data2$compliance <- ifelse(data2$compliance=="Almost always", "1 (always)",
                     ifelse(data2$compliance=="Often", "2",
                            ifelse(data2$compliance=="Sometimes", "3",
                                   ifelse(data2$compliance =="Almost never", "4 (never)",
                                          NA))))


data2$ch <- ifelse(data2$c_alphan=="CH", "1",0)
data2$ch <- factor(data2$ch, levels=c(0:1), labels=c("Industrial Countries", "Switzerland"))
world<- subset(data2, ch=="Industrial Countries")
ch <- subset(data2, ch=="Switzerland")

##############################################
# Binarise variables, calculate confidence intervals
##############################################

# Switzerland
reg_d <- as.numeric(ch$regulation == "1 (for)" | ch$regulation == "2")
reg_d <- na.omit(reg_d)
reg_ci_s <- binom.confint(sum(reg_d=="1"), length(reg_d), methods="exact")
reg_ci_f <- binom.confint(sum(reg_d=="0"), length(reg_d), methods="exact")

spend_d <- as.numeric(ch$spending == "1 for" | ch$spending == "2")
spend_d <- na.omit(spend_d)
spend_ci_s <- binom.confint(sum(spend_d=="1"), length(spend_d), methods="exact")
spend_ci_f <- binom.confint(sum(spend_d=="0"), length(spend_d), methods="exact")

law_d <- as.numeric(ch$laws == "1 (disagree)" | ch$laws == "2")
law_d <- na.omit(law_d)
law_ci_s <- binom.confint(sum(law_d=="1"), length(law_d), methods="exact")
law_ci_f <- binom.confint(sum(law_d=="0"), length(law_d), methods="exact")

comp_d <- as.numeric(ch$compliance == "1 (always)" | ch$compliance == "2")
comp_d <- na.omit(comp_d)
comp_ci_s <- binom.confint(sum(comp_d=="1"), length(comp_d), methods="exact")
comp_ci_f <- binom.confint(sum(comp_d=="0"), length(comp_d), methods="exact")

ci_ch_reg <- rbind(reg_ci_s, reg_ci_f)
ci_ch <- rbind(ci_ch_reg, law_ci_s, law_ci_f, spend_ci_s, spend_ci_f, comp_ci_s, comp_ci_f)
ci_ch$ctr <- "Switzerland"
ci_ch$grp <- rep(c("Agree", "Disagree", "Con", "Pro", "Less", "More", "Often", "Rarely"), each=nrow(reg_ci_s))
ci_ch$item <- rep(c("Less regulation in general", "Evt.laws on industry", "Govt.spending on evt.", "Firms comply w/ rules"), each=nrow(ci_ch_reg))


# Other high-income countries
reg_d <- as.numeric(world$regulation == "1 (for)" | world$regulation == "2")
reg_d <- na.omit(reg_d)
reg_ci_s <- binom.confint(sum(reg_d=="1"), length(reg_d), methods="exact")
reg_ci_f <- binom.confint(sum(reg_d=="0"), length(reg_d), methods="exact")

spend_d <- as.numeric(world$spending == "1 for" | world$spending == "2")
spend_d <- na.omit(spend_d)
spend_ci_s <- binom.confint(sum(spend_d=="1"), length(spend_d), methods="exact")
spend_ci_f <- binom.confint(sum(spend_d=="0"), length(spend_d), methods="exact")

law_d <- as.numeric(world$laws == "1 (disagree)" | world$laws == "2")
law_d <- na.omit(law_d)
law_ci_s <- binom.confint(sum(law_d=="1"), length(law_d), methods="exact")
law_ci_f <- binom.confint(sum(law_d=="0"), length(law_d), methods="exact")

comp_d <- as.numeric(world$compliance == "1 (always)" | world$compliance == "2")
comp_d <- na.omit(comp_d)
comp_ci_s <- binom.confint(sum(comp_d=="1"), length(comp_d), methods="exact")
comp_ci_f <- binom.confint(sum(comp_d=="0"), length(comp_d), methods="exact")

ci_ic_reg <- rbind(reg_ci_s, reg_ci_f)
ci_ic <- rbind(ci_ic_reg, law_ci_s, law_ci_f, spend_ci_s, spend_ci_f, comp_ci_s, comp_ci_f)
ci_ic$ctr <- "High-income countries"
ci_ic$grp <- rep(c("Agree", "Disagree", "Con", "Pro", "Less", "More", "Often", "Rarely"), each=nrow(reg_ci_s))
ci_ic$item <- rep(c("Less regulation in general", "Evt.laws on industry", "Govt.spending on evt.", "Firms comply w/ rules"), each=nrow(ci_ic_reg))


##############################################
# Build and save Figure A.8
##############################################
df_ci_combo <- rbind(ci_ch, ci_ic)
df_ci_combo$grp <- factor(df_ci_combo$grp)
df_ci_combo$ctr <- factor(df_ci_combo$ctr)
df_ci_combo$item <- factor(df_ci_combo$item)


combo_d <- ggplot(df_ci_combo, aes(x=grp, y = mean, group = ctr, color=ctr), size=1.2)+
  geom_pointrange(aes(min = lower, max = upper), position = position_dodge(width=.5), size=1.2) +
  theme_bw() + 
  coord_flip() +
  ylab("Proportion") +
  xlab("Attributes") +
  geom_hline(yintercept = 0.5, lty="dashed") + 
  theme(strip.text.x = element_text(face="bold", size=12))+
  scale_colour_brewer(palette= "Dark2") +
  scale_shape_discrete(name= c()) +
  theme(strip.background = element_rect(fill = 'white'))+
  theme(axis.title.x = element_text(face="bold", size=16),
        axis.text.x  = element_text(face="bold",size=12, colour="black"))+
  theme(axis.title.y = element_text(face="bold", size=16),
        axis.text.y  = element_text(face="bold",size=12, colour ="black"))+
  theme(legend.title = element_blank())+
  theme(legend.text = element_text(size=10, face="bold"))+
  theme(panel.grid.major = element_line(colour = "grey75", size=0.5))+
  facet_grid(item~., scales = "free")+
  theme(panel.grid.major.x = element_line(size=.3, color="gray80"),
        panel.grid.minor.x = element_blank(),
        panel.grid.major.y = element_line(size=.3, color="gray80" ),
        panel.grid.minor.y = element_blank(),
        legend.position="bottom")
combo_d

#setwd(...)

tiff("FigA8.tiff", units="in", width=8, height = 8, res=200)
combo_d
dev.off()



##############################################
# FIGURE A.7 - SEP DATA PUBLICLY AVAILABLE - ONLY CODE REQUIRED TO GENERATE - SEE README.TXT
##############################################

up <- read.dta13("NAME OF SEP DATA.dta")
kvi <- read.csv("ecscore_rep.csv")


#CALCULATE ENVIRONMENTAL CONCERN IN SEP DATA
up$w1_q9x6 <- as.numeric(revalue(as.character(up$w1_q9x6), c("5"="1", "4"="2", "3"="3", "2"="4", "1"="5")))
df_ecs <- up[c("pid","w1_q9x1", "w1_q9x2", "w1_q9x3", "w1_q9x4", "w1_q9x5", "w1_q9x6", "w1_q9x7", "w1_q9x8", "w1_q9x9")]


colSums(!is.na(df_ecs))

df_ecs$ecsum <- df_ecs$w1_q9x1+ df_ecs$w1_q9x2+ df_ecs$w1_q9x3+df_ecs$w1_q9x4+df_ecs$w1_q9x5+df_ecs$w1_q9x6+df_ecs$w1_q9x7+ 
  df_ecs$w1_q9x8+ df_ecs$w1_q9x9

df_ecs$d1 <- ifelse(is.na(df_ecs$w1_q9x1), 0, 1)
df_ecs$d2 <- ifelse(is.na(df_ecs$w1_q9x2), 0, 1)
df_ecs$d3 <- ifelse(is.na(df_ecs$w1_q9x3), 0, 1)
df_ecs$d4 <- ifelse(is.na(df_ecs$w1_q9x4), 0, 1)
df_ecs$d5 <- ifelse(is.na(df_ecs$w1_q9x5), 0, 1)
df_ecs$d6 <- ifelse(is.na(df_ecs$w1_q9x6), 0, 1)
df_ecs$d7 <- ifelse(is.na(df_ecs$w1_q9x7), 0, 1)
df_ecs$d8 <- ifelse(is.na(df_ecs$w1_q9x8), 0, 1)
df_ecs$d9 <- ifelse(is.na(df_ecs$w1_q9x9), 0, 1)

df_ecs$respno <- df_ecs$d1+ df_ecs$d2+ df_ecs$d3+ df_ecs$d4+ df_ecs$d5 + df_ecs$d6+ df_ecs$d7+ df_ecs$d8+ df_ecs$d9
df_ecs$ecscore <- df_ecs$ecsum/df_ecs$respno


df_ecs <- df_ecs[c(
  "pid", "ecscore"
)]

up <- join(x=df_ecs, y=up, by="pid")


# CALCULATION OF ENVIRONMENTAL CONCERN SCORE IN SAMPLE DATA BASED ON Q60_1 TO Q60_9 IN MASTER FILE, SAME PROCEDURE AS FOR THE SEP DATA ABOVE

kvi_ec_df <- data.frame(kvi$ecscore)
kvi_ec_df$type <- "Intervista"
kvi_ec_df=rename(kvi_ec_df, c("kvi.ecscore"="ecscore"))

up_ec_df <- data.frame(up$ecscore)
up_ec_df$type <- "SEP"
up_ec_df=rename(up_ec_df, c("up.ecscore"="ecscore"))

combo_df <- rbind(kvi_ec_df, up_ec_df)


combo_df$ecfac<- ifelse(combo_df$ecscore>=1&combo_df$ecscore<1.5, "1",                       
                        ifelse(combo_df$ecscore>=1.5&combo_df$ecscore<2.5,"2",
                               ifelse(combo_df$ecscore>=2.5&combo_df$ecscore<3.5, "3",
                                      ifelse(combo_df$ecscore>=3.5&combo_df$ecscore<4.5, "4",
                                             ifelse(combo_df$ecscore>=4.5&combo_df$ecscore<=5, "5",
                                                    NA)))))

ecscore_plot <- ggplot(subset(combo_df, !is.na(ecfac)), aes(ecfac, group = type)) + 
  geom_bar(aes(y = ..prop.., fill = type), stat="count", position="dodge") + 
  scale_y_continuous(labels=scales::percent) +
  scale_x_discrete(breaks=seq(1,5, by=1))+
  scale_fill_brewer(palette="Set1")+
  theme(axis.title.x = element_text(face="bold", size=14, colour="black"),
        axis.text.x  = element_text(face="bold",size=14, colour="black"))+
  theme(axis.title.y = element_text(face="bold", size=14, colour="black"),
        axis.text.y  = element_text(face="bold",size=14, colour="black"))+
  ylab("Share in percent") +
  xlab("Environmental concern")+
  theme(legend.title =  element_blank())+
  theme(legend.text = element_text(size=12, face="bold"))

ecscore_plot

ggsave("FigA7.png", plot=ecscore_plot)

